package com.server.controller;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.server.common.base.PathManager;
import com.server.common.base.ProjectInfor;
import com.server.common.base.SessionData;
import com.server.service.ISourceService;
import com.server.service.IUserManagementService;
import com.server.web.DataVector;
import com.server.web.JSONSerielizer;
import com.server.web.OutputUtil;
import com.server.web.RequestInput;
import com.server.web.SessionUtil;

@Controller
@RequestMapping("user")
public class UserManagementController {

	@Autowired
	private IUserManagementService userManagementService;
	@Autowired
	private ISourceService sourceService;
	
	@RequestMapping("")
	public ModelAndView userPage(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("user/userList");
		mv.addObject("projectName",ProjectInfor.PLATFORM_NAME);
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		mv.addObject("userLevel",SessionUtil.getIntAttri(request, SessionData.levelKey));
		mv.addObject("userCard",SessionUtil.getStringAttri(request, SessionData.cardIdKey));
		return mv;
	}
	@RequestMapping("list/{orgNo}/{state}")
	public void list(HttpServletRequest request,
			HttpServletResponse response,@PathVariable String orgNo,
			@PathVariable Integer state){
		Integer levelKey = SessionUtil.getIntAttri(request, SessionData.levelKey);
		RequestInput requestInput = new RequestInput(request);
		DataVector<Map<String, Object>> listData = userManagementService.getList(requestInput,orgNo,state,levelKey);
		OutputUtil.returnGridData(response, listData.getCount(), JSONSerielizer.toJson(userManagementService.replaceSourceName(listData.getList()), null, null));
	}
	@RequestMapping("source/{typeId}")
	public void source(HttpServletRequest request,
			HttpServletResponse response,@PathVariable Integer typeId){
		DataVector<Map<String, Object>> listData = sourceService.getList(null, typeId);
		List<Map<String, Object>> list = listData.getList();
		Iterator<Map<String, Object>> iterator = list.iterator();
		while(iterator.hasNext()){
			Map<String, Object> next = iterator.next();
			Object nameObj = next.get("UserName");
			Object phoneObj = next.get("PhoneNo");
			if (nameObj != null && phoneObj != null) {
				next.put("UserName", nameObj.toString()+"（"+phoneObj.toString()+"）");
			}
			
		}
		OutputUtil.returnString(response, JSONSerielizer.toJson(list, null, null));
	}
	@RequestMapping("add/{orgNo}")
	public ModelAndView add(HttpServletRequest request,
			HttpServletResponse response, @PathVariable String orgNo) {
		String userName = SessionUtil.getStringAttri(request, SessionData.nameKey);
		ModelAndView mv = new ModelAndView("common/editFrame");
		mv.addObject("pagePath","pages/user/userForm.jsp");
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		mv.addObject("action", "new");
		mv.addObject("business", "user");
		mv.addObject("titleName", "员工入职");
		mv.addObject("orgNo", orgNo);
		mv.addObject("userName", userName);
		
		mv.addObject("configStr", userManagementService.getConfigStr());
		
		StringBuffer serverBuffer = new StringBuffer();
		String serverName = request.getServerName();
		int serverPort = request.getServerPort();
		serverBuffer.append("http://").append(serverName)
		.append(":").append(serverPort).append("/")
		.append(PathManager.FILE_MAPPING);
		mv.addObject("fileServer",serverBuffer.toString());
		
		return mv;
	}
	@RequestMapping("edit/{employId}")
	public ModelAndView edit(HttpServletRequest request,
			HttpServletResponse response,@PathVariable String employId) {
		String userName = SessionUtil.getStringAttri(request, SessionData.nameKey);
		ModelAndView mv = new ModelAndView("common/editFrame");
		mv.addObject("pagePath","pages/user/userForm.jsp");
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		mv.addObject("action", "edit");
		mv.addObject("business", "user");
		mv.addObject("id", employId);
		mv.addObject("titleName", "修改员工信息");
		mv.addObject("userName", userName);
		
		mv.addObject("configStr", userManagementService.getConfigStr());
		
		StringBuffer serverBuffer = new StringBuffer();
		String serverName = request.getServerName();
		int serverPort = request.getServerPort();
		serverBuffer.append("http://").append(serverName)
		.append(":").append(serverPort).append("/")
		.append(PathManager.FILE_MAPPING);
		mv.addObject("fileServer",serverBuffer.toString());
		return mv;
	}
	@RequestMapping("check/{employId}")
	public ModelAndView check(HttpServletRequest request,
			HttpServletResponse response,@PathVariable String employId) {
		String userName = SessionUtil.getStringAttri(request, SessionData.nameKey);
		ModelAndView mv = new ModelAndView("common/editFrame");
		mv.addObject("pagePath","pages/user/userForm.jsp");
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		mv.addObject("action", "check");
		mv.addObject("business", "user");
		mv.addObject("id", employId);
		mv.addObject("titleName", "查看员工信息");
		mv.addObject("userName", userName);
		
		mv.addObject("configStr", userManagementService.getConfigStr());
		
		StringBuffer serverBuffer = new StringBuffer();
		String serverName = request.getServerName();
		int serverPort = request.getServerPort();
		serverBuffer.append("http://").append(serverName)
		.append(":").append(serverPort).append("/")
		.append(PathManager.FILE_MAPPING);
		mv.addObject("fileServer",serverBuffer.toString());
		return mv;
	}
	@RequestMapping("form/{employId}")
	public void form(HttpServletRequest request,
			HttpServletResponse response,@PathVariable String employId){
		Map<String, Object> formData = userManagementService.getForm(employId);
		OutputUtil.returnString(response, JSONSerielizer.toJson(formData, null, null));
	}
	@RequestMapping("save/{action}")
	public void save(HttpServletRequest request,
			HttpServletResponse response,@PathVariable String action){
		String operator = SessionUtil.getStringAttri(request, SessionData.cardIdKey);
		String formData = request.getParameter("formData");
		Map<String, Object> inputMap = JSONSerielizer.toMap(formData);
		
		Map<String, Object> map = new HashMap<String, Object>();
		if ("new".equals(action) || "edit".equals(action)) {
			map = userManagementService.addAndEmploy(operator, inputMap, action);
		}else {
			
		}
		OutputUtil.returnString(response, JSONSerielizer.toJson(map, null, null));
	}
	@RequestMapping("employ/{state}")
	public void employ(HttpServletRequest request,
			HttpServletResponse response, @PathVariable Integer state){
		String operator = SessionUtil.getStringAttri(request, SessionData.cardIdKey);
		String employIds = request.getParameter("employIds");
		Boolean isSuccess = false;
		if (state.equals(0)) {
			isSuccess = userManagementService.delete(operator,employIds);
		}else if (state.equals(1)) {
			isSuccess = userManagementService.employ(operator,employIds);
		}
		OutputUtil.returnSuccess(response, isSuccess);
	}
	@RequestMapping("checkIn")
	public void checkIn(HttpServletRequest request,
			HttpServletResponse response){
		String operator = SessionUtil.getStringAttri(request, SessionData.cardIdKey);
		String employIds = request.getParameter("employIds");
		String dayStr = request.getParameter("days");
		Float days = 0.0F;
		if (!StringUtils.isEmpty(dayStr)) {
			days = Float.parseFloat(dayStr);
		}
		Boolean isSuccess = userManagementService.checkIn(operator, employIds, days);
		OutputUtil.returnSuccess(response, isSuccess);
	}
	@RequestMapping("info/{cardId}")
	public void info(HttpServletRequest request,
			HttpServletResponse response,@PathVariable String cardId){
		Map<String, Object> formData = userManagementService.getFormByCard(cardId);
		OutputUtil.returnString(response, JSONSerielizer.toJson(formData, null, null));
	}
	@RequestMapping("checkInDetail/{employId}")
	public ModelAndView checkInDetail(HttpServletRequest request,@PathVariable Integer employId){
		ModelAndView mv = new ModelAndView("user/checkInDetail");
		mv.addObject("projectName",ProjectInfor.PLATFORM_NAME);
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		mv.addObject("employId",employId);
		return mv;
	}
	@RequestMapping("checkInList/{employId}")
	public void checkInList(HttpServletRequest request,
			HttpServletResponse response,@PathVariable Integer employId){
		RequestInput requestInput = new RequestInput(request);
		DataVector<Map<String, Object>> listData = userManagementService.getCheckInList(requestInput, employId);
		OutputUtil.returnGridData(response, listData.getCount(), JSONSerielizer.toJson(listData.getList(), null, null));
	}
}
